﻿///说明是注释或者比较原始的语句  //表示注释
///e.g.,举例

///格式化方法 e.g.,"{0}".format("some")
String.prototype.format = function () {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g,
        function (m, i) {
            return args[i];
        });
}
    ///序列化为字符串方法 e.g.,String.fnSerialize({name:zhengyong})
    ///obj是json对象   提示：反序列化用$.parseJSON("{}")
    String.fnSerialize = function (obj) {
    ///编码特殊字符
    var transferCharForJavascript = function (s) {
        var newStr = s.replace(
        /[\x26\x27\x3C\x3E\x0D\x0A\x22\x2C\x5C\x00]/g,function (c) {
            var ascii = c.charCodeAt(0)
            return '\\u00' + (ascii < 16 ? '0' + ascii.toString(16) : ascii.toString(16))
        }
        );
        return newStr;
    }

    if (obj == null) {
        return null
    }
    else if (obj.constructor == Array) {
        var builder = [];
        builder.push("[");
        for (var index in obj) {
            if (typeof obj[index] == "function") continue;
            if (index > 0) builder.push(",");
            if (typeof obj[index] == "string") {
                var sFormatArrayString = "\"{0}\"";
                builder.push(sFormatArrayString.format(transferCharForJavascript(obj[index])));
            } else {
                builder.push(String.fnSerialize(obj[index]));
            }
            ///builder.push(String.fnSerialize(obj[index]));
        }
        builder.push("]");
        return builder.join("");
    }
    else if (obj.constructor == Object) {
        var builder2 = [];
        builder2.push("{");
        var index = 0;
        for (var key in obj) {
            if (typeof obj[key] == "function") continue;
            if (index > 0) builder2.push(",");
            ///var sFormatObject = "\"{0}\":{1}"; //原始
            var sFormatObject = "\"{0}\":{1}";
            var sFormatObjectString = "\"{0}\":\"{1}\"";
            if (typeof obj[key] == "string") {
                builder2.push(sFormatObjectString.format(key, transferCharForJavascript(obj[key])));
            } else {
                builder2.push(sFormatObject.format(key, String.fnSerialize(obj[key])));
            }
            ///builder2.push(sFormatObject.format(key, String.fnSerialize(obj[key])));//原来只有这一句话
            index++;
        }
        builder2.push("}");
        return builder2.join("");
    }
    else if (obj.constructor == Boolean) {
        return obj.toString();
    }
    else if (obj.constructor == Number) {
        return obj.toString();
    }
    else if (obj.constructor == String) {
        var sFormatString = "{0}";
        return sFormatString.format(transferCharForJavascript(obj));
    }
    else if (obj.constructor == Date) {
        ///var sFormatDate = '{"__DataType":"Date","__thisue":{0}}';
        var sFormatDate = "{0}";
        return sFormatDate.format(obj.getTime() - (new Date(1970, 0, 1, 0, 0, 0)).getTime()); //将返回长整型日期如："1271260800000"
    }
    else if (this.toString != undefined) {
        return String.fnSerialize(obj);
    }
}
///解析后台的如 \/Date(1271260800000)\/或/Date(1271260800000)/ 的日期为 指定格式的日期字符串
///e.g.,parseServerDate("后台日期：\/Date(1271260800000)\/")
function parseServerDate(source, reg, sDateFormat) {
    var longDate = "$1";
    reg = reg ? reg : /\\\/Date\((\d+)\)\\\//ig;
    var regReg=/\/Date\((\d+)\)\//ig;//不带反斜杠的匹配
    sDateFormat = sDateFormat ? sDateFormat : 'yy-mm-dd';
    if (reg.test(source)) {
        source = source.replace(reg,
                    function (sMatch, longDate) {
                        return $.datepicker.formatDate(sDateFormat, new Date(parseInt(longDate)));
                    }
                );
    } else if (regReg.test(source)) {
                    source = source.replace(regReg,
                    function (sMatch, longDate) {
                        return $.datepicker.formatDate(sDateFormat, new Date(parseInt(longDate)));
                    }
                );
    }
    return source;
}
///提供的默认的Ajax回掉函数，处理带有日期类型的后台数据 
///e.g.,配置为"fnServerData":fnServerDataDefaultWithDate
function fnServerDataDefaultWithDate(sUrl, aoData, fnCallback, oSettings) {
    oSettings.jqXHR = $.ajax({
        "url": sUrl,
        "data": aoData,
        "success": function (json) {
            $(oSettings.oInstance).trigger('xhr', oSettings);
            if (json.aaData == null && typeof (json.aaDataStr) == "string") {
                json.aaDataStr = parseServerDate(json.aaDataStr);
                json.aaData = $.parseJSON(json.aaDataStr);
            } else if (json.aaData != null) {
                json.aaDataStr = parseServerDate(String.fnSerialize(json.aaData));
                json.aaData = $.parseJSON(json.aaDataStr);
            }
            fnCallback(json);
        },
        "dataType": "json",
        "cache": false,
        "type": oSettings.sServerMethod,
        "error": function (xhr, error, thrown) {
            if (error == "parsererror") {
                alert("DataTables warning: JSON data from server could not be parsed. " +
							"This is caused by a JSON formatting error.");
            }
        }
    });
}
///提供的默认的Ajax回掉函数，不需要处理日期类型的后台数据 
///e.g.,配置为"fnServerData":fnServerDataDefault
function fnServerDataDefault(sUrl, aoData, fnCallback, oSettings) {
    oSettings.jqXHR = $.ajax({
        "url": sUrl,
        "data": aoData,
        "success": function (json) {
            $(oSettings.oInstance).trigger('xhr', oSettings);
            if (json.aaData == null && typeof (json.aaDataStr) == "string") {                
                json.aaData = $.parseJSON(json.aaDataStr);
            }
            fnCallback(json);
        },
        "dataType": "json",
        "cache": false,
        "type": oSettings.sServerMethod,
        "error": function (xhr, error, thrown) {
            if (error == "parsererror") {
                alert("DataTables warning: JSON data from server could not be parsed. " +
							"This is caused by a JSON formatting error.");
            }
        }
    });
}

var oLanguageDefault = {
    "sProcessing": "信息处理中,请稍后...",
    "sLengthMenu": "每页_MENU_ 条",
    "sZeroRecords": "对不起，没有数据.",
    "sInfo": "第 _START_ 到 _END_ 条，共 _TOTAL_ 条",
    "sInfoEmpty": "第 0 到 0 条，共 0 条",
    "sInfoFiltered": "(从 _MAX_ 条筛选)",
    "sInfoPostFix": "",
    "sSearch": "搜索",
    "sUrl": "",
    "oPaginate": { "sFirst": "第一页", "sPrevious": "上一页", "sNext": "下一页", "sLast": "最后一页" }
}